package com.bifang.core.dict.gateway.controller;

import cn.hutool.core.convert.Convert;
import com.bifang.common.constant.Validators;
import com.bifang.common.vo.AntTreeVO;
import com.bifang.common.vo.AppResponse;
import com.bifang.common.vo.BaseVO;
import com.bifang.common.vo.DictDeleteVO;
import com.bifang.common.vo.DictFieldRetVO;
import com.bifang.common.vo.DictVO;
import com.bifang.common.vo.SelectVO;
import com.bifang.common.vo.dict.SearchQueryVO;
import com.bifang.common.vo.dict.SelectQueryVO;
import com.bifang.common.vo.dict.SelectResultVO;
import com.bifang.core.dict.gateway.req.AreaSelectReq;
import com.bifang.core.dict.gateway.req.DictDeleteReq;
import com.bifang.core.dict.gateway.req.DictDetailReq;
import com.bifang.core.dict.gateway.req.DictFieldReq;
import com.bifang.core.dict.gateway.req.DictReq;
import com.bifang.core.dict.gateway.req.SelectReq;
import com.bifang.core.dict.gateway.validators.DictValidators;
import com.bifang.core.dict.integration.DictIntegration;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数据字典管理
 *
 * @author chenzh
 * @date 2019-112-10
 */
@RestController
@RequestMapping(value = "/dict")
@Api(tags = "数据字典管理")
public class DictContorller {
    @Autowired DictIntegration dictIntegration;
    /** 分页查询数据字典信息 */
    @PostMapping(value = "/query/list", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "数据字典列表", notes = "分页查询数据字典信息")
    public AppResponse<PageInfo<DictVO>> getPageList(@RequestBody DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        return AppResponse.successData(dictIntegration.getDictList(dictVO));
    }
    /** 查询单个详情 */
    @ApiOperation(value = "数据字典详情", notes = "根据id查询详情")
    @PostMapping(value = "/query/detail", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> getDetail(
            @RequestBody @Validated(value = {Validators.GetDetail.class}) DictDetailReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        return AppResponse.successData(dictIntegration.getDictDetail(dictVO));
    }
    /** 获取字典字段下拉 */
    @ApiOperation(value = "数据字典字段下拉", notes = "获取所有字典字段下拉")
    @PostMapping(value = "common/query/dictField", produces = "application/json;charset=UTF-8")
    public AppResponse<DictFieldRetVO> getDictField(@RequestBody DictFieldReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        return AppResponse.successData(dictIntegration.getDictField(dictVO));
    }
    /** 新增 */
    @ApiOperation(value = "新增字典", notes = "新增字典信息")
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> add(
            @RequestBody @Validated(value = {Validators.Add.class}) DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        return AppResponse.successData(dictIntegration.add(dictVO));
    }
    /** 修改 */
    @ApiOperation(value = "修改字典", notes = "修改字典信息")
    @PostMapping(value = "/edit", produces = "application/json;charset=UTF-8")
    public AppResponse<DictVO> edit(
            @RequestBody @Validated(value = {Validators.Edit.class}) DictReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        return AppResponse.successData(dictIntegration.edit(dictVO));
    }
    /** 删除 */
    @ApiOperation(value = "单个删除字典", notes = "单个删除字典信息")
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public AppResponse delete(
            @RequestBody @Validated(value = {Validators.Del.class}) DictDeleteReq dictReq) {
        DictDeleteVO dictVO = Convert.convert(DictDeleteVO.class, dictReq);
        dictIntegration.delete(dictVO);
        return AppResponse.success();
    }
    /** 删除 批量 */
    @ApiOperation(value = "批量删除字典", notes = "批量删除字典信息")
    @PostMapping(value = "/delete/batch", produces = "application/json;charset=UTF-8")
    public AppResponse deleteBatch(
            @RequestBody @Validated(value = {Validators.BatchDel.class}) DictDeleteReq dictReq) {
        DictDeleteVO dictVO = Convert.convert(DictDeleteVO.class, dictReq);
        dictIntegration.deleteBatch(dictVO);
        return AppResponse.success();
    }
    /** 清除缓存 */
    @ApiOperation(value = "清除数据字典缓存", notes = "清除数据字典缓存，不传入参数则清除所有")
    @PostMapping(value = "/clear/cache", produces = "application/json;charset=UTF-8")
    public AppResponse clearCache(@RequestBody DictFieldReq dictReq) {
        DictVO dictVO = Convert.convert(DictVO.class, dictReq);
        dictIntegration.clearCache(dictVO);
        return AppResponse.success();
    }
    /** select 初始化 批量 */
    @ApiOperation(value = "批量获取字典下拉信息", notes = "批量获取字典下拉信息")
    @PostMapping(value = "/common/batch", produces = "application/json;charset=UTF-8")
    public AppResponse<Map<String, List<SelectVO>>> getDictBatch(
            @RequestBody @Validated(value = {DictValidators.GetDictBatch.class})
                    SelectReq selectReq) {
        return AppResponse.successData(dictIntegration.getDictBatch(selectReq.getDictFields()));
    }
    /** select 初始化单个 */
    @ApiOperation(value = "单个获取字典下拉信息", notes = "单个获取字典下拉信息")
    @PostMapping(value = "/common/one", produces = "application/json;charset=UTF-8")
    public AppResponse<List<SelectVO>> getDict(
            @RequestBody @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        SelectVO selectVO = Convert.convert(SelectVO.class, selectReq);
        return AppResponse.successData(dictIntegration.getDict(selectVO));
    }

    /** select 初始化单个过滤 */
    @ApiOperation(value = "单个获取过滤条件字典下拉信息", notes = "单个获取过滤条件字典下拉信息")
    @PostMapping(value = "/common/condition", produces = "application/json;charset=UTF-8")
    public AppResponse<List<SelectVO>> getDictCondition(
            @RequestBody @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        SelectVO selectVO = Convert.convert(SelectVO.class, selectReq);
        return AppResponse.successData(
                dictIntegration.getDictCondition(selectVO, selectReq.getDictKeys()));
    }

    /** 通用数据字典下拉树 */
    @ApiOperation(value = "通用获取字典下拉树", notes = "通用获取字典下拉树")
    @PostMapping(value = "/common/tree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getDictTree(
            @RequestBody @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        return AppResponse.successData(dictIntegration.getDictCommonTree(selectReq.getDictField()));
    }

    /** 带条件的通用数据字典下拉树 */
    @ApiOperation(value = "带条件的通用获取字典下拉树", notes = "带条件的通用获取字典下拉树")
    @PostMapping(value = "/common/condition/tree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getConditionDictTree(
            @RequestBody @Validated(value = {DictValidators.GetDict.class}) SelectReq selectReq) {
        SelectVO selectVO = Convert.convert(SelectVO.class, selectReq);
        return AppResponse.successData(dictIntegration.getDictConditionTree(selectVO));
    }

    /** 全国行政区划下拉树 */
    @ApiOperation(value = "获取全国行政区划下拉树", notes = "获取全国行政区划下拉树")
    @PostMapping(value = "/common/areaSelectTree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getAreaSelectTree(
            @RequestBody AreaSelectReq areaSelectReq) {
        return AppResponse.successData(
                dictIntegration.getAreaSelectTree(
                        areaSelectReq.getInData(), areaSelectReq.getNotINData()));
    }

    /** 下拉图标树 */
    @ApiOperation(value = "获取下拉图标树", notes = "获取下拉图标树")
    @PostMapping(value = "/common/iconSelectTree", produces = "application/json;charset=UTF-8")
    public AppResponse<List<AntTreeVO>> getIconSelectTree(@RequestBody BaseVO baseVO) {
        return AppResponse.successData(dictIntegration.getIconSelectTree(baseVO));
    }

    @PostMapping(value = "/common/getSelectData", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取下拉菜单数据", notes = "获取下拉菜单数据")
    public AppResponse<List<SelectResultVO>> getSelectData(
            @RequestBody @Validated SelectQueryVO vo) {
        return AppResponse.successData(dictIntegration.getSelectData(vo));
    }

    @PostMapping(value = "/common/getSearchData", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取通用搜索数据", notes = "获取通用搜索数据")
    public AppResponse<PageInfo<?>> getCommonData(@RequestBody @Validated SearchQueryVO vo) {
        return AppResponse.successData(dictIntegration.getCommonData(vo));
    }
}
